#ifndef DECODER_HPP
#define DECODER_HPP

#define MAX_CHANNELS 8

#include <malloc.h>
#include <stdint.h>
#include <string>
#include <samplerate.h> //to change frequency: http://www.mega-nerd.com/SRC/api_simple.html
#include <iostream>
#include <cmath>
#include <samplerate.h>

/// @brief Interface for the different decoders
class Decoder {
    protected:
        int handle; //file handle
        unsigned short channels;
        unsigned int frequency, desiredFrequency; //or samplerate
        uint64_t samples; //number of samples
        unsigned int position;
        bool seekable;
        std::string errorMessage;
        /** @brief Get the number of frame to decode if the file samplerate is diffrent from the playback system samplerate
         * @param outFrames Desired number of frames in the buffer
         * @return Number of frame to decode
         */
        unsigned int getFrameToDecode(unsigned int outFrames);
        SRC_STATE* sState;
        void resample(SRC_DATA *data);
    public:
        Decoder();
        ~Decoder();
        /** @brief Open the file for reading
         * @param filename name and its path
         * @param silence If enabled, no processing otherwise, the decoder will detect silences at start and at the end
         * @return true on opening succes, otherwise false
         */
        virtual bool load(const char* filename, bool silence=false) = 0;
        /** @brief Close the file
         */
        virtual void close() = 0;
        /** @brief Decode audio data
         * @param data Contains decoded audio
         * @param bufferSize Number of frame to decode
         * @return Decoded item count
         */
        virtual int decode(float* data, unsigned long frames) = 0;
        /** @brief seek Seek to the given frame
         * @param time Frame index
         * @return True on success
         */
        virtual bool seek(unsigned int frame) = 0;
        /** @brief Return the number of audio channels
         */
        unsigned short getChannelCount();
        /** @brief Return the audio frequency or samplerate in Hz
         */
        unsigned int getFrequency();
        /** @brief Return the audio frequency or samplerate in Hz
         */
        unsigned int getSampleRate(); //same as getFrequency
        /** @brief Return the number of audio samples
         */
        uint64_t getSampleCount();
        const char* getErrorMessage();
        /** @brief This is use for the playback system that need a defined samplerate
         * @param f Frequency/samplerate in Hz
         * @note This is not used and implemented in the different decoders yet
         */
        void setDesiredFrequency(unsigned int f);
    //signals:
        //void updateTime(unsigned int ms);
};

#endif // DECODER_HPP
